package excel

import (
	"encoding/json"
	"errors"
)

func Transform(input interface{}, fields []string) ([][]interface{}, error) {
	b, err := json.Marshal(input)
	if err != nil {
		return nil, errors.New("excel.Transform:cannot be Marshal")
	}
	var data []map[string]interface{}

	err = json.Unmarshal(b, &data)
	if err != nil {
		return nil, errors.New("excel.Transform:can not be Unmarshal")
	}
	var res [][]interface{}

	for _, v := range data {
		var tmp []interface{}
		for _, field := range fields {
			if value, ok := v[field]; ok {
				tmp = append(tmp, value)
			}
		}
		res = append(res, tmp)
	}
	return res, nil
}
